home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Very Newbie question on pointers
- Date: 6 Mar 1996 21:34:14 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hlsgmINNcgt@keats.ugrad.cs.ubc.ca>
- References: <bantolov-0603960018080001@bantolov.seanet.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <bantolov-0603960018080001@bantolov.seanet.com>,
- Bruce Antolovich <bantolov@bantolov.seanet.com> wrote:
- >I hope that this is the right place to post such a question; if not, sorry
- >for the wasted bandspace.
- >
- >I am very new to programming in C but have a large background in FORTRAN.
- >(please no snickers) I am having a problem with the memory location of
- >variables as described by pointers. I define several variables and try to
- >get their address in memory. Unfortunately, my code gives me the same
- >address for all variables! Any clues as to where I'm going wrong would be
- >very appreciated.
- >
- >I don't think that it matters but I'm using Metrowerks CW on a PowerMac 6100/60.
-
- It does. You are using %d in printf() to print pointer values. This is not
- right because pointers are not integers, and certainly don't even have to be
- the same bit size.
-
- It's possible that the ``int'' type on your (POWER PC) compiler is 32 bits, and
- that pointers are 64 bit. If that is the case, printf("%d",pointer_var) will
- just look at the top 32 bits of the 64-bit pointer. Of course these will likely
- be the same for two variables that are declared side by side in the same stack
- frame!
-
- Of course, this is just an explanation of what might be happening in your
- program---I certainly don't mean to imply that if you are on a system where
- both ints and all pointers are the same size, you are free to treat them as
- interchangeable. The conversion of integral types to pointers and back is
- governed by an implementation-defined mapping. The integral type has to have a
- sufficient size to hold the pointer (this size is implementation defined). If
- you convert a pointer to a sufficiently wide integer, and then back to a
- pointer of the same type, you recover the pointer. (K&R2 A6.6)
-
- To print pointer values, the proper conversion argument for printf() is
- "%p", not "%d".
-
- Hope this helps!
-
- BTW, Fortran is cool! :)
- --
-
-